using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.DesktopSearch;

namespace DesktopSearch
{
    public partial class DesktopSearch : Form
    {

        #region Globle variable
        //Use for storing previous tab name.
        public string lastTab;

        //use for counting files while searching.
        public int count = 0;

        //Use for storing tab name while changing tab.
        public string Tabname;

        ///Creating object of DesktopSearchManager Class.
        DesktopSearchManager desktopsearch = new DesktopSearchManager();

        #endregion

        #region Form initialization

        /// <summary>
        /// Constructor. Keeps GUI in particular state at start.
        /// </summary>
        public DesktopSearch()
        {
            InitializeComponent();


            grpAdvanceFileSearchOptions.Enabled = false;

            //891, 652
            InitializeAllTab();
            InitializePictureTab();
            InitializeMusicTab();
            InitializeEmailTab();
            InitializeMsOfficeTab();


            //Setting default root for folderBrowserDialog.
            fdSerach.RootFolder = System.Environment.SpecialFolder.Desktop;

        }

        /// <summary>
        /// Keeps controls of Tab-page 'All' in initial state.
        /// </summary>
        public void InitializeAllTab()
        {
            //Set Loction textBox as empty.
            txtAllLocation.Text = null;
            //Setting all combo box to zero Index.
            cmbAllDate.SelectedIndex = 1;
            cmbAllDateCompare.SelectedIndex = 0;
            cmbAllSize.SelectedIndex = 0;
            CmbFileTypes.SelectedIndex = 0;
            //clear all header of list view.
            lsvSearchResults.Columns.Clear();

            //Setting new Header to list view.
            lsvSearchResults.Columns.Add("File Name", 120);
            lsvSearchResults.Columns.Add("Location", 150);
            lsvSearchResults.Columns.Add("Author", 100);
            lsvSearchResults.Columns.Add("Size(B)", 100);
            lsvSearchResults.Columns.Add("Attribute", 100);
            lsvSearchResults.Columns.Add("Date Created", 100);
            lsvSearchResults.Columns.Add("Date Modified", 100);
            lsvSearchResults.Columns.Add("Title", 100);
            //Setting default value of last tab as All.
            lastTab = "All";

        }

        /// <summary>
        /// Keeps controls of Tab-page 'Email' in initial state.
        /// </summary>
        public void InitializeEmailTab()
        {
            //Setting all combo box to zero Index.
            cmbEmailDate.SelectedIndex = 1;
            cmbEmailDateCompare.SelectedIndex = 0;
            cmbEmailSize.SelectedIndex = 0;
            cmbEmailLocation.SelectedIndex = 0;
            cmbEmailPriority.SelectedIndex = 0;

        }

        /// <summary>
        /// Keeps controls of Tab-page 'Picture' in initial state.
        /// </summary>
        public void InitializePictureTab()
        {
            //Setting all combo box to zero Index.
            cmbPictureDate.SelectedIndex = 1;
            cmbPictureDateCompare.SelectedIndex = 0;
            cmbPictureSize.SelectedIndex = 0;

        }

        /// <summary>
        /// Keeps controls of Tab-page 'Music' in initial state.
        /// </summary>
        public void InitializeMusicTab()
        {
            //Setting all combo box to zero Index.
            cmbMusicDate.SelectedIndex = 1;
            cmbMusicDateCompare.SelectedIndex = 0;
            cmbMusicSize.SelectedIndex = 0;

        }

        /// <summary>
        /// Keeps controls of Tab-page Document in initial state.
        /// </summary>
        public void InitializeMsOfficeTab()
        {
            //Setting all combo box to zero Index.
            cmbMsOfficeDate.SelectedIndex = 1;
            cmbMsOfficeDateCompare.SelectedIndex = 0;
            cmbMsOfficeSize.SelectedIndex = 0;
            cmbMsOfficeFiles.SelectedIndex = 0;
        }

        #endregion

        #region Methods
        /// <summary>
        /// File search method.
        /// </summary>
        public void AllFileSearch()
        {

            ///clear lable_count text.
            lblCount.Text = "";

            ///Creating object of FileSearchCriteria  Class.
            FileSearchCriteria fileSearchCriteria = new FileSearchCriteria();

            //Setting  propeties value of FileSearchCriteria for specifying search Criteria.
            fileSearchCriteria.FileAuthor = txtAllAuthor.Text;
            fileSearchCriteria.FileType = (FileType)CmbFileTypes.SelectedIndex;
            fileSearchCriteria.FileName = txtAllName.Text;
            fileSearchCriteria.FileTag = txtAllTag.Text;
            fileSearchCriteria.SearchLocation = txtAllLocation.Text;
            fileSearchCriteria.FileDateFilter = (FileDateFilter)cmbAllDate.SelectedIndex;
            fileSearchCriteria.DateCompareOption = (DateCompareOption)cmbAllDateCompare.SelectedIndex;

            ///If date filter is not Any then set value for date. 
            if (cmbAllDateCompare.SelectedIndex != 0)
            {
                fileSearchCriteria.Date = dtAll.Value;
            }

            fileSearchCriteria.SizeCompareOption = (SizeCompareOption)cmbAllSize.SelectedIndex;

            ///If size text box is not empty then setting size value.
            if (txtAllSize.Text != string.Empty)
            {
                float size = 0;
                if (!Single.TryParse(txtAllSize.Text, out size))
                {
                    MessageBox.Show("Only integer is acceptable");
                    return;
                }

                fileSearchCriteria.Size = Convert.ToUInt32(Math.Round(size));
            }

            ///If extension text box is not empty then setting extension value.
            if (txtAllExtension.Text != "")
            {
                fileSearchCriteria.Extension = txtAllExtension.Text;

            }

            ///remove all items from list view.
            lsvSearchResults.Items.Clear();

            try
            {

                ///Calling search method.
                List<FileInfo> files = desktopsearch.Search(fileSearchCriteria);

                ///check if no result found.
                if (files.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;
                    ///Setting FileInfo properties to ListView.
                    foreach (FileInfo file in files)
                    {
                        string authorList = String.Empty;
                        if (file.Author != null)
                        {
                            foreach (string author in file.Author)
                            {
                                authorList += author + " ;";
                            }
                        }
                        string[] fields = new string[]
                        {
                            file.Name,
                            file.SearchLocation,
                            authorList,
                            file.Size.ToString(),
                            file.Attributes.ToString(),
                            file.DateCreated.ToShortDateString(),
                            file.DateModified.ToShortDateString(),
                            file.Title
                        };
                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;

                    }
                    lblCount.Text = count.ToString() + " files found !";

                }
            }

            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }
            //Catch and show ArgumentException.
            catch (ArgumentException ex)
            {
                MessageBox.Show("Input is not valid !");

                string exception = ex.ToString();
            }
            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }
        }

        /// <summary>
        /// Music search method.
        /// </summary>
        public void MusicSearch()
        {
            //clear lable_count text.
            lblCount.Text = "";

            //Creating object of MusicSearchCriteria Class.
            MusicSearchCriteria musicSearchCriteria = new MusicSearchCriteria();

            //Setting  propeties value of MusicSearchCriteria for specifying search Criteria.
            musicSearchCriteria.AlbumName = txtMusicAlbum.Text;
            musicSearchCriteria.ArtistName = txtMusicArtist.Text;
            musicSearchCriteria.SearchLocation = txtMusicLocation.Text;
            musicSearchCriteria.MusicTitle = txtMusicTitle.Text;
            musicSearchCriteria.MusicDateFilter = (MusicDateFilter)cmbMusicDate.SelectedIndex;
            musicSearchCriteria.DateCompareOption = (DateCompareOption)cmbMusicDateCompare.SelectedIndex;

            //If date filter is not Any then set value for date. 
            if (cmbMusicDateCompare.SelectedIndex != 0)
            {
                musicSearchCriteria.Date = dtMusic.Value;
            }

            musicSearchCriteria.SizeCompareOption = (SizeCompareOption)cmbMusicSize.SelectedIndex;

            //If size text box is not empty then setting size value.
            if (txtMusicSize.Text != string.Empty)
            {
                float size = 0;
                if (!Single.TryParse(txtMusicSize.Text, out size))
                {
                    MessageBox.Show("Only integer is acceptable");
                    return;
                }

                musicSearchCriteria.Size = Convert.ToUInt32(Math.Round(size));

            }

            ///If extension text box is not empty then setting extension value.
            if (txtMusicExtension.Text != "")
            {
                musicSearchCriteria.Extension = txtMusicExtension.Text;
            }

            //remove all items from list view.
            lsvSearchResults.Items.Clear();

            try
            {
                //Calling search method.
                List<MusicInfo> musics = desktopsearch.Search(musicSearchCriteria);

                //check if no result found.
                if (musics.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;
                    //Setting FileInfo properties to ListView.
                    foreach (MusicInfo music in musics)
                    {
                        string artistList = String.Empty;
                        if (music.Artists != null)
                        {
                            foreach (string artist in music.Artists)
                            {
                                artistList += artist + "; ";
                            }
                        }
                        string[] fields = new string[]
                        {
                            music.Name,
                            music.Album,
                            artistList,
                            music.SearchLocation,
                            music.Size.ToString(),
                            music.Rating.ToString(),
                            music.Attributes.ToString()
                        };

                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;
                    }
                    lblCount.Text = count.ToString() + " music files found !";

                }
            }
            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }
            //Catch and show ArgumentException.
            catch (ArgumentException ex)
            {
                MessageBox.Show("Input is not valid !");

                string exception = ex.ToString();
            }
            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }
        }

        /// <summary>
        /// Picture search method.
        /// </summary>
        public void PictureSearch()
        {
            //clear lable_count text.
            lblCount.Text = "";

            //Creating object of PictureSearchCriteria Class.
            PictureSearchCriteria pictureSearchCriteria = new PictureSearchCriteria();
            pictureSearchCriteria.PictureName = txtPictureName.Text;
            pictureSearchCriteria.SearchLocation = txtPictureLocation.Text;
            pictureSearchCriteria.PictureTag = txtPictureTag.Text;
            pictureSearchCriteria.PictureTitle = txtPictureTitle.Text;
            pictureSearchCriteria.PictureDateFilter = (PictureDateFilter)cmbPictureDate.SelectedIndex;
            pictureSearchCriteria.DateCompareOption = (DateCompareOption)cmbPictureDateCompare.SelectedIndex;

            //If date filter is not Any then set value for date. 
            if (cmbPictureDateCompare.SelectedIndex != 0)
            {
                pictureSearchCriteria.Date = dtPicture.Value;
            }

            pictureSearchCriteria.SizeCompareOption = (SizeCompareOption)cmbPictureSize.SelectedIndex;

            //If size text box is not empty then setting size value.
            if (txtPictureSize.Text != string.Empty)
            {

                float size = 0;
                if (!Single.TryParse(txtPictureSize.Text, out size))
                {
                    MessageBox.Show("Only integer is acceptable");
                    return;
                }

                pictureSearchCriteria.Size = Convert.ToUInt32(Math.Round(size));

            }
            ///If extension text box is not empty then setting extension value.
            if (txtPictureExtension.Text != "")
            {
                pictureSearchCriteria.Extension = txtPictureExtension.Text;
            }

            //remove all items from list view.
            lsvSearchResults.Items.Clear();


            try
            {
                //Calling search method.
                List<PictureInfo> pictures = desktopsearch.Search(pictureSearchCriteria);

                //check if no result found.
                if (pictures.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;

                    //Setting FileInfo properties to ListView.
                    foreach (PictureInfo picture in pictures)
                    {
                        //Local variable for storing dateTaken set as empty.
                        string dateTaken = string.Empty;

                        //Checks if dateTaken is not 01/01/0001.
                        if (!picture.DateTaken.ToShortDateString().Equals("01-01-0001"))
                        {
                            dateTaken = picture.DateTaken.ToShortDateString();
                        }
                        string tagList = String.Empty;
                        if (picture.Tags != null)
                        {
                            foreach (string tag in picture.Tags)
                            {
                                tagList += tag + "; ";
                            }
                        }
                        string[] fields = new string[]
                           {
                              picture.Name,
                              picture.SearchLocation,
                              dateTaken,
                              picture.Size.ToString(),
                              tagList,
                              picture.Rating.ToString(),
                              picture.Attributes.ToString(),
                           };


                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;

                    }
                    //sets file count to label.
                    lblCount.Text = count.ToString() + " files found !";

                }
            }

            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }
            //Catch and show ArgumentException.
            catch (ArgumentException ex)
            {
                MessageBox.Show("Input is not valid !");

                string exception = ex.ToString();
            }
            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }

        }

        /// <summary>
        /// Email search method.
        /// </summary>
        public void EmailSearch()
        {
            //clear lable_count text.
            lblCount.Text = "";

            EmailSearchCriteria emailSearchCriteria = new EmailSearchCriteria();

            //Setting  propeties value of EmailSearchCriteria for specifying search Criteria.


            if (cmbEmailLocation.SelectedIndex == 0)
            {
                //Set serch location for All Index.
                emailSearchCriteria.SearchLocation = "Personal folders";
            }
            else
            {
                emailSearchCriteria.SearchLocation = cmbEmailLocation.SelectedItem.ToString();

            }
            emailSearchCriteria.EmailTo = txtEmailTo.Text;
            emailSearchCriteria.EmailFrom = txtEmailFrom.Text;
            emailSearchCriteria.EmailSubject = txtEmailSubject.Text;
            emailSearchCriteria.EmailDateFilter = (EmailDateFilter)cmbEmailDate.SelectedIndex;
            emailSearchCriteria.DateCompareOption = (DateCompareOption)cmbEmailDateCompare.SelectedIndex;
            emailSearchCriteria.EmailAttachments = txtAttachments.Text;
            int priority = cmbEmailPriority.SelectedIndex;
            if (priority > 1)
            {
                switch (priority)
                {
                    case 2:
                        priority = 3;
                        break;
                    case 3:
                        priority = 5;
                        break;
                }
            }
            emailSearchCriteria.Priority = (EmailPriority)priority;
            //If date filter is not Any then set value for date. 
            if (cmbEmailDateCompare.SelectedIndex != 0)
            {
                emailSearchCriteria.Date = dtEmail.Value;
            }

            emailSearchCriteria.SizeCompareOption = (SizeCompareOption)cmbEmailSize.SelectedIndex;

            //If size text box is not empty then setting size value.
            if (txtEmailSize.Text != string.Empty)
            {
                float size = 0;
                if (!Single.TryParse(txtEmailSize.Text, out size))
                {
                    MessageBox.Show("Only integer is acceptable");
                    return;
                }

                emailSearchCriteria.Size = Convert.ToUInt32(Math.Round(size));

            }

            //remove all items from list view.
            lsvSearchResults.Items.Clear();

            try
            {
                //Calling search method.
                List<EmailInfo> emails = desktopsearch.Search(emailSearchCriteria);


                //check if no result found. 
                if (emails.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;
                    //Setting FileInfo properties to ListView.
                    foreach (EmailInfo email in emails)
                    {
                        string attachmentList = String.Empty;
                        if (email.Attachments != null)
                        {
                            foreach (string attachment in email.Attachments)
                            {
                                attachmentList += attachment + "; ";
                            }
                        }
                        string toList = String.Empty;
                        if (email.ToList != null)
                        {
                            foreach (string id in email.ToList)
                            {
                                toList += id + "; ";
                            }
                        }
                        string hasAttachments = "No";
                        if (email.HasAttachments)
                        {
                            hasAttachments = "Yes";

                        }
                        string[] fields = new string[]
                        {
                            email.From,
                            email.Subject,
                            email.DateReceived.ToShortDateString(),
                            toList,
                            email.Size.ToString(),
                            hasAttachments ,
                            attachmentList,
                            email.Priority.ToString()
                        };
                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;

                    }
                    //sets file count to label.
                    lblCount.Text = count.ToString() + " files found !";
                }
            }
            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }
            //Catch and show ArgumentException.
            catch (ArgumentException ex)
            {
                MessageBox.Show("Input is not valid !");

                string exception = ex.ToString();
            }
            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }


        }

        /// <summary>
        ///Document search method.
        /// </summary>
        public void MsOfficeFileSearch()
        {
            //clear lable_count text.
            lblCount.Text = "";

            //Creating object of FileSearchCriteria  Class.
            FileSearchCriteria fileSearchCriteria = new FileSearchCriteria();

            //Setting  propeties value of FileSearchCriteria for specifying search Criteria.
            fileSearchCriteria.FileAuthor = txtMsOfficeAuthor.Text;
            fileSearchCriteria.FileType = (FileType)(cmbMsOfficeFiles.SelectedIndex + 1);
            fileSearchCriteria.FileName = txtMsOfficeName.Text;
            fileSearchCriteria.FileTag = txtMsOfficeTag.Text;
            fileSearchCriteria.SearchLocation = txtMsOfficeLocation.Text;
            fileSearchCriteria.FileDateFilter = (FileDateFilter)cmbMsOfficeDate.SelectedIndex;
            fileSearchCriteria.DateCompareOption = (DateCompareOption)cmbMsOfficeDateCompare.SelectedIndex;

            //If date filter is not Any then set value for date. 
            if (cmbMsOfficeDateCompare.SelectedIndex != 0)
            {
                fileSearchCriteria.Date = dtMsOffice.Value;
            }

            fileSearchCriteria.SizeCompareOption = (SizeCompareOption)cmbMsOfficeSize.SelectedIndex;

            //If size text box is not empty then setting size value.
            if (txtMsOfficeSize.Text != string.Empty)
            {
                float size = 0;
                if (!Single.TryParse(txtMsOfficeSize.Text, out size))
                {
                    MessageBox.Show("Only integer is acceptable");
                    return;
                }

                fileSearchCriteria.Size = Convert.ToUInt32(Math.Round(size));

            }

            //remove all items from list view.
            lsvSearchResults.Items.Clear();

            try
            {
                //Calling search method.
                List<FileInfo> files = desktopsearch.Search(fileSearchCriteria);

                //check if no result found.
                if (files.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;
                    //Setting FileInfo properties to ListView.
                    foreach (FileInfo file in files)
                    {
                        string authorList = String.Empty;
                        if (file.Author != null)
                        {
                            foreach (string author in file.Author)
                            {
                                authorList += author + "; ";
                            }
                        }
                        string[] fields = new string[]
                        {
                            file.Name,
                            file.SearchLocation,
                            authorList,
                            file.Size.ToString(),
                            file.Attributes.ToString(),
                            file.DateCreated.ToShortDateString(),
                            file.DateModified.ToShortDateString(),
                            file.Title
                        };
                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;

                    }

                    //sets file count to label.
                    lblCount.Text = count.ToString() + " files found !";
                }
            }
            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }
            //Catch and show ArgumentException.
            catch (ArgumentException ex)
            {
                MessageBox.Show("Input is not valid !");

                string exception = ex.ToString();
            }
            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }
        }

        /// <summary>
        ///Files search by file name.
        /// </summary>
        public void FileSearch()
        {

            ///clear lable_count text.
            lblCount.Text = "";
            // Get file name specified in input.
            string fileName = txtFileName.Text;

            try
            {

                ///Calling search method.
                List<FileInfo> files = desktopsearch.Search(fileName);

                ///check if no result found.
                if (files.Count == 0)
                {
                    lblCount.Text = "No files found !";
                }
                else
                {
                    count = 0;
                    ///Setting FileInfo properties to ListView.
                    foreach (FileInfo file in files)
                    {
                        string authorList = String.Empty;
                        if (file.Author != null)
                        {
                            foreach (string author in file.Author)
                            {
                                authorList += author + " ;";
                            }
                        }
                        string[] fields = new string[]
                        {
                            file.Name,
                            file.SearchLocation,
                            authorList,
                            file.Size.ToString(),
                            file.Attributes.ToString(),
                            file.DateCreated.ToShortDateString(),
                            file.DateModified.ToShortDateString(),
                            file.Title
                        };
                        //Creating new listview item.
                        ListViewItem item = new ListViewItem(fields);

                        //Adds new item to listview.
                        lsvSearchResults.Items.Add(item);

                        //Increment counter.
                        count++;

                    }
                    lblCount.Text = count.ToString() + " files found !";

                }
            }

            //Catch and show ArgumentNullException.
            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Input can not be null !");

                string exception = ex.ToString();
            }

            //Catch and show DesktopSearchException
            catch (DesktopSearchException ex)
            {
                MessageBox.Show("Error ! Search can not be perform");

                string exception = ex.ToString();
            }
        }

        #endregion

        #region Event

        #region Tab change event
        /// <summary>
        /// Keeps controle on tab change event.
        /// </summary>
        private void tbSearch_Selected(object sender, TabControlEventArgs e)
        {
            //Takes tab name.
            Tabname = e.TabPage.Name;

            //Sets listview headers and default settings for tab.
            switch (Tabname)
            {
                case "Email":
                    {


                        //remove all items from list view.
                        lsvSearchResults.Items.Clear();

                        //clear all header of list view.
                        lsvSearchResults.Columns.Clear();

                        //Setting new Header to list view.
                        lsvSearchResults.Columns.Add("From", 120);
                        lsvSearchResults.Columns.Add("Subject", 100);
                        lsvSearchResults.Columns.Add("Date Receive", 100);
                        lsvSearchResults.Columns.Add("To Name", 180);
                        lsvSearchResults.Columns.Add("Size(B)", 100);
                        lsvSearchResults.Columns.Add("Has Attachment", 100);
                        lsvSearchResults.Columns.Add("Attachments", 100);
                        lsvSearchResults.Columns.Add("Priority", 100);
                    }
                    break;

                case "All":
                    {

                        //remove all items from list view.
                        lsvSearchResults.Items.Clear();

                        //clear all header of list view.
                        lsvSearchResults.Columns.Clear();

                        //Setting new Header to list view.
                        lsvSearchResults.Columns.Add("File Name", 120);
                        lsvSearchResults.Columns.Add("Location", 150);
                        lsvSearchResults.Columns.Add("Author", 100);
                        lsvSearchResults.Columns.Add("Size(B)", 100);
                        lsvSearchResults.Columns.Add("Attribute", 100);
                        lsvSearchResults.Columns.Add("Date Created", 100);
                        lsvSearchResults.Columns.Add("Date Modified", 100);
                        lsvSearchResults.Columns.Add("Title", 100);

                        //set loction value form preivious tab.
                        switch (lastTab)
                        {
                            case "Music":
                                {
                                    txtAllLocation.Text = txtMusicLocation.Text;
                                }

                                break;

                            case "Picture":
                                {
                                    txtAllLocation.Text = txtPictureLocation.Text;
                                }

                                break;

                            case "Document":
                                {
                                    txtAllLocation.Text = txtMsOfficeLocation.Text;
                                }

                                break;
                        }
                        //Sets last tab as current tab.
                        lastTab = "All";
                    }
                    break;
                case "Music":
                    {


                        //remove all items from list view.
                        lsvSearchResults.Items.Clear();

                        //clear all header of list view.
                        lsvSearchResults.Columns.Clear();

                        //Setting new Header to list view.
                        lsvSearchResults.Columns.Add("Name", 120);
                        lsvSearchResults.Columns.Add("Album Name", 120);
                        lsvSearchResults.Columns.Add("Artist Name", 100);
                        lsvSearchResults.Columns.Add("Location", 150);
                        lsvSearchResults.Columns.Add("Size(B)", 100);
                        lsvSearchResults.Columns.Add("Rating", 100);
                        lsvSearchResults.Columns.Add("Attribute", 100);

                        //set loction value form preivious tab.
                        switch (lastTab)
                        {
                            case "All":
                                {
                                    txtMusicLocation.Text = txtAllLocation.Text;
                                }

                                break;

                            case "Picture":
                                {
                                    txtMusicLocation.Text = txtPictureLocation.Text;
                                }

                                break;

                            case "Document":
                                {
                                    txtMusicLocation.Text = txtMsOfficeLocation.Text;
                                }

                                break;
                        }

                        //Sets last tab as current tab.
                        lastTab = "Music";

                    }

                    break;

                case "Picture":
                    {

                        //remove all items from listview.
                        lsvSearchResults.Items.Clear();

                        //clear all header of listview.
                        lsvSearchResults.Columns.Clear();

                        //Setting new Header to listview.
                        lsvSearchResults.Columns.Add("Name", 120);
                        lsvSearchResults.Columns.Add("Location", 150);
                        lsvSearchResults.Columns.Add("Date Taken", 100);
                        lsvSearchResults.Columns.Add("Size(B)", 100);
                        lsvSearchResults.Columns.Add("Tag", 100);
                        lsvSearchResults.Columns.Add("Rating", 100);
                        lsvSearchResults.Columns.Add("Attribute", 100);

                        //Set loction value form preivious tab.
                        switch (lastTab)
                        {

                            case "All":
                                {
                                    txtPictureLocation.Text = txtAllLocation.Text;
                                }

                                break;

                            case "Music":
                                {
                                    txtPictureLocation.Text = txtMusicLocation.Text;
                                }

                                break;

                            case "Document":
                                {
                                    txtPictureLocation.Text = txtMsOfficeLocation.Text;
                                }

                                break;

                        }
                        //Sets last tab as current tab.
                        lastTab = "Picture";


                    }
                    break;
                case "Document":
                    {
                        //remove all items from listview.
                        lsvSearchResults.Items.Clear();

                        //clear all header of listview.
                        lsvSearchResults.Columns.Clear();

                        //Setting new Header to listview.
                        lsvSearchResults.Columns.Add("File Name", 120);
                        lsvSearchResults.Columns.Add("Location", 150);
                        lsvSearchResults.Columns.Add("Author", 100);
                        lsvSearchResults.Columns.Add("Size(B)", 100);
                        lsvSearchResults.Columns.Add("Attribute", 100);
                        lsvSearchResults.Columns.Add("Date Created", 100);
                        lsvSearchResults.Columns.Add("Date Modified", 100);
                        lsvSearchResults.Columns.Add("Title", 100);

                        //listViewSearchResults.w

                        //set loction value form preivious tab.
                        switch (lastTab)
                        {

                            case "Music":
                                {
                                    txtMsOfficeLocation.Text = txtMusicLocation.Text;
                                }

                                break;

                            case "Picture":
                                {
                                    txtMsOfficeLocation.Text = txtPictureLocation.Text;
                                }

                                break;

                            case "All":
                                {
                                    txtMsOfficeLocation.Text = txtAllLocation.Text;
                                }

                                break;
                        }

                        //Sets last tab as current tab.
                        lastTab = "Document";
                    }
                    break;
            }
        }
        #endregion

        #region Enable or disable Datetime picker

        /// <summary>
        /// Enable or disable Datetime picker for all file search.
        /// When date compare is any then makes it disable.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbAllDateCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbAllDateCompare.SelectedIndex == 0)
            {
                dtAll.Enabled = false;
            }
            else
            {
                dtAll.Enabled = true;
            }
        }

        /// <summary>
        /// Enable or disable Datetime picker for Email search.
        /// When date compare is any then makes it disable.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbEmailDateCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbEmailDateCompare.SelectedIndex == 0)
            {
                dtEmail.Enabled = false;
            }
            else
            {
                dtEmail.Enabled = true;
            }

        }

        /// <summary>
        /// Enable or disable Datetime picker for Document search.
        /// When date compare is any then makes it disable.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMsOfficeDateCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbMsOfficeDateCompare.SelectedIndex == 0)
            {
                dtMsOffice.Enabled = false;
            }
            else
            {
                dtMsOffice.Enabled = true;
            }

        }

        /// <summary>
        /// Enable or disable Datetime picker for Picture search.
        /// When date compare is any then makes it disable.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPictureDateCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbPictureDateCompare.SelectedIndex == 0)
            {
                dtPicture.Enabled = false;
            }
            else
            {
                dtPicture.Enabled = true;
            }

        }

        /// <summary>
        /// Enable or disable Datetime picker for Music search.
        /// When date compare is any then makes it disable.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMusicDateCompare_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbMusicDateCompare.SelectedIndex == 0)
            {
                dtMusic.Enabled = false;
            }
            else
            {
                dtMusic.Enabled = true;
            }

        }
        #endregion

        #region Enable or disable Size TextBox

        /// <summary>
        /// Keeps control on enable or disable Size textBox for All File search.
        /// If size index is zero then makes it disale and set null value to size textbox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbAllSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            //check if selected index is 0. 
            if (cmbAllSize.SelectedIndex == 0)
            {
                txtAllSize.Text = null;
                txtAllSize.Enabled = false;
            }
            else
            {
                txtAllSize.Enabled = true;
            }
        }

        /// <summary>
        /// Keeps control on enable or disable Size textBox for Email search.
        /// If size index is zero then makes it disale and set null value to size text box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbEmailSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbEmailSize.SelectedIndex == 0)
            {
                txtEmailSize.Text = null;
                txtEmailSize.Enabled = false;
            }
            else
            {
                txtEmailSize.Enabled = true;
            }

        }

        /// <summary>
        /// Keeps control on enable or disable Size textBox for Document search.
        /// If size index is zero then makes it disale and set null value to size textbox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMsOfficeSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMsOfficeSize.SelectedIndex == 0)
            {
                txtMsOfficeSize.Text = null;
                txtMsOfficeSize.Enabled = false;
            }
            else
            {
                txtMsOfficeSize.Enabled = true;
            }

        }

        /// <summary>
        /// Keeps control on enable or disable Size textBox for Picture search.
        /// If size index is zero then makes it disale and set null value to text box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPictureSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPictureSize.SelectedIndex == 0)
            {
                txtPictureSize.Text = null;
                txtPictureSize.Enabled = false;
            }
            else
            {
                txtPictureSize.Enabled = true;
            }

        }

        /// <summary>
        /// Keeps control on enable or disable Size textBox for Music search.
        /// If size index is zero then makes it disale and set null value to text box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMusicSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMusicSize.SelectedIndex == 0)
            {
                txtMusicSize.Text = null;
                txtMusicSize.Enabled = false;
            }
            else
            {
                txtMusicSize.Enabled = true;
            }

        }

        #endregion

        #region Button click event for searching.

        ///Search button click event for file Search by only file name.
        private void btnSearchFile_Click(object sender, EventArgs e)
        {
            lsvSearchResults.Items.Clear();
            try
            {
                //Set cursor as wait cursor.
                Cursor.Current = Cursors.WaitCursor;


                //Calling FileSearch method.
                FileSearch();

                //Set cursor as default cursor.
                Cursor.Current = Cursors.Default;
            }
            catch
            {
                //Set cursor as default cursor.
                Cursor.Current = Cursors.Default;
            }
        }

        ///Search button click event for all files Search.
        private void btnSearchAll_Click(object sender, EventArgs e)
        {

            //check textbox value of size is not empty when size compare option selected.
            if (cmbAllSize.SelectedIndex != 0 && txtAllSize.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please Enter size !");
                return;
            }
            else
            {
                try
                {
                    //Set cursor as wait cursor.
                    Cursor.Current = Cursors.WaitCursor;

                    //Calling AllFileSearch method.
                    AllFileSearch();

                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
                catch
                {
                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
            }
        }

        ///Search button click event for Music Search.
        private void btnSearchMusic_Click(object sender, EventArgs e)
        {
            //check textbox value of size is not empty when size compare option selected.
            if (cmbMusicSize.SelectedIndex != 0 && txtMusicSize.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please Enter size !");
                return;
            }
            else
            {
                try
                {
                    //Set cursor as wait cursor.
                    Cursor.Current = Cursors.WaitCursor;

                    //Calling MusicSearch method.
                    MusicSearch();

                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
                catch
                {
                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
            }
        }

        ///Search button click event for email Search.
        private void btnEmail_Click(object sender, EventArgs e)
        {

            //check textbox value of size is not empty when size compare option selected.
            if (cmbEmailSize.SelectedIndex != 0 && txtEmailSize.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please Enter size !");
                return;
            }
            else
            {
                try
                {
                    //Set cursor as wait cursor.
                    Cursor.Current = Cursors.WaitCursor;

                    //Calling EmailSearch method.
                    EmailSearch();

                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
                catch
                {
                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
            }

        }

        ///Search button click event for picture Search.
        private void btnSearchPicture_Click(object sender, EventArgs e)
        {
            //check textbox value of size is not empty when size compare option selected.
            if (cmbPictureSize.SelectedIndex != 0 && txtPictureSize.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please Enter size !");
                return;
            }
            else
            {
                try
                {
                    //Set cursor as wait cursor.
                    Cursor.Current = Cursors.WaitCursor;

                    //Calling PictureSearch method.
                    PictureSearch();

                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
                catch
                {
                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }

            }

        }


        ///Search button click event for MS office file Search.
        private void btnSearchMsOffice_Click(object sender, EventArgs e)
        {
            //check textbox value of size is not empty when size compare option selected.
            if (cmbMsOfficeSize.SelectedIndex != 0 && txtMsOfficeSize.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please Enter size !");
                return;
            }
            else
            {
                try
                {
                    //Set cursor as wait cursor.
                    Cursor.Current = Cursors.WaitCursor;

                    //Calling DocumentSearch method.
                    MsOfficeFileSearch();

                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
                catch
                {
                    //Set cursor as default cursor.
                    Cursor.Current = Cursors.Default;
                }
            }


        }

        #endregion

        #region Select Loction Button click Event

        ///Set loction from FolderBrowser to textbox  for All tab.
        private void btnAllLoction_Click(object sender, EventArgs e)
        {
            fdSerach.ShowDialog();
            txtAllLocation.Text = fdSerach.SelectedPath;
        }

        ///Set loction from FolderBrowser to textbox  for Music tab.
        private void btnMusicLocation_Click(object sender, EventArgs e)
        {
            fdSerach.ShowDialog();
            txtMusicLocation.Text = fdSerach.SelectedPath;
        }

        ///Set loction from FolderBrowser to textbox  for Picture tab.
        private void btnPictureLocation_Click(object sender, EventArgs e)
        {
            fdSerach.ShowDialog();
            txtPictureLocation.Text = fdSerach.SelectedPath;
        }

        ///Set loction from FolderBrowser to textbox  for Document tab.
        private void btnMsOfficeLocation_Click(object sender, EventArgs e)
        {
            fdSerach.ShowDialog();
            txtMsOfficeLocation.Text = fdSerach.SelectedPath;
        }
        #endregion

        #region  Enable or disable DateCompare ComboBox when date is None

        /// <summary>
        /// Keeps Control on activating and deactivating DateCompare ComboBox if date is None.
        /// </summary>


        //Activating and deactivating DateCompare ComboBox for All.
        private void cmbAllDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAllDate.SelectedIndex == 0)
            {
                cmbAllDateCompare.SelectedIndex = 0;
                cmbAllDateCompare.Enabled = false;
            }
            else
            {
                cmbAllDateCompare.Enabled = true;
            }
        }

        //Activating and deactivating DateCompare ComboBox for Email.
        private void comboBoxEmailDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbEmailDate.SelectedIndex == 0)
            {
                cmbEmailDateCompare.SelectedIndex = 0;
                cmbEmailDateCompare.Enabled = false;
            }
            else
            {
                cmbEmailDateCompare.Enabled = true;
            }

        }

        //Activating and deactivating DateCompare ComboBox for Document.
        private void comboBoxDocumentDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMsOfficeDate.SelectedIndex == 0)
            {
                cmbMsOfficeDateCompare.SelectedIndex = 0;
                cmbMsOfficeDateCompare.Enabled = false;
            }
            else
            {
                cmbMsOfficeDateCompare.Enabled = true;
            }
        }

        //Activating and deactivating DateCompare ComboBox for Picture.
        private void comboBoxPictureDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPictureDate.SelectedIndex == 0)
            {
                cmbPictureDateCompare.SelectedIndex = 0;
                cmbPictureDateCompare.Enabled = false;
            }
            else
            {
                cmbPictureDateCompare.Enabled = true;
            }

        }

        //Activating and deactivating DateCompare ComboBox for Music.
        private void comboBoxMusicDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMusicDate.SelectedIndex == 0)
            {
                cmbMusicDateCompare.SelectedIndex = 0;
                cmbMusicDateCompare.Enabled = false;
            }
            else
            {
                cmbMusicDateCompare.Enabled = true;
            }
        }

        #endregion

        #region check box check event for selecting advanced search for files.
        ///Set search options for specfic type like email, picture and music etc if checked
        ///otherwise faciliates searching performed by only file name.
        private void chkAdvancedSearch_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAdvancedSearch.Checked == true)
            {
                txtFileName.Text = "";
                txtFileName.Enabled = false;
                btnSearchFile.Enabled = false;
                grpAdvanceFileSearchOptions.Enabled = true;

            }
            else
            {
                //Setting new Header to list view.
                lsvSearchResults.Columns.Clear();
                lsvSearchResults.Columns.Add("File Name", 120);
                lsvSearchResults.Columns.Add("Location", 150);
                lsvSearchResults.Columns.Add("Author", 100);
                lsvSearchResults.Columns.Add("Size(B)", 100);
                lsvSearchResults.Columns.Add("Attribute", 100);
                lsvSearchResults.Columns.Add("Date Created", 100);
                lsvSearchResults.Columns.Add("Date Modified", 100);
                lsvSearchResults.Columns.Add("Title", 100);
                //Setting default value of last tab as All.
                lastTab = "All";

                txtFileName.Enabled = true;
                btnSearchFile.Enabled = true;
                grpAdvanceFileSearchOptions.Enabled = false;



            }
        }
        #endregion


        #endregion






















    }
}